<?php
namespace app\admin\controller;
use think\Db;
use think\Page;
use app\api\model\Income;
use app\api\model\RegionCommission;
use app\api\model\Team1Commission;
use app\api\model\Team2Commission;
use think\paginator\driver\Bootstrap;
class Profit extends Common
{
//任务发布
    public function profit_list(){

        $phone = input('phone');

        if(!empty($phone)){

            $where1 = "phone like '%$phone%'";
            $uid = db('user_account')->field('id')->where($where1)->find();
            $where = 'uid ='.$uid['id'];
            $this->assign('phone', $phone);
        }else{
            $where = '';
        }

    $IncomeModel = new Income;
    $RegionModel = new RegionCommission;
    $Team1Model = new Team1Commission;
    $Team2Model = new Team2Commission;

    $Income = "id,uid,created_at,'money' as commission,'个人任务佣金' as commission_type";

    $Region = "id,uid,created_at,commission,'区域代理佣金' as commission_type";

    $team1 = "id,uid,created_at,commission,if(type = 1,'二级任务佣金','二级会员升级佣金') as commission_type";

    $team2 = "id,uid,created_at,commission,if(type = 1,'一级任务佣金','一级会员升级佣金') as commission_type";

    $IncomeUnion = $IncomeModel
                        -> field($Income)
                        -> where($where)
                        -> where('status',2)
                       // -> where($whereDate)
                        -> buildSql();

    $RegionUnion = $RegionModel
                        -> field($Region)
                        -> where($where)
                       // -> where($whereDate)
                        -> buildSql();

    $Team1Union = $Team1Model
                        -> field($team1)
                        -> where($where)
                       // -> where($whereDate)
                        -> buildSql();
                                            
    $Team2Union = $Team2Model
                        -> field($team2)
                        -> where($where)
                        //-> where($whereDate)
                        -> union([$IncomeUnion,$RegionUnion,$Team1Union])
                        -> buildSql();

    $data = DB::table($Team2Union.'ty_team2_commission')
                        -> order('created_at','DESC')
                        -> paginate(15,false,['query'=>request()->param()])
                        ->each(function($item,$key){
                            $id = $item['uid'];

                            $user = db('user_account')->field('head_url,nick_name,real_name,phone,province,city,county,level')->where('id',$id)->find();

                            $item['head_url'] = $user['head_url'];
                            $item['nick_name'] = $user['nick_name'];
                            $item['real_name'] = $user['real_name'];
                            $item['phone'] = $user['phone'];
                            $item['province'] = $user['province'];
                            $item['city'] = $user['city'];
                            $item['county'] = $user['county'];
                            $item['level'] = $user['level'];

                            return $item;
                        });

                      
    // 获取分页显示
    $page = $data->render();
    // 模板变量赋值

    $this->assign('page', $page);
    $this->assign('data', $data);
    return $this->fetch();
 }


    //导出收益记录
    public function profit_export(){

        $account = input('account');

        if(!empty($account)){
            $where = 'u.account='.$account;
        }else{
            $where = '';
        }

        $team1_commission = db('team1_commission')->alias('t')->field('t.*,u.head_url,u.nick_name,u.real_name,u.phone,u.province,u.city,u.county,u.level,u.account')->join('user_account u','u.id=t.uid')->where($where)->order('t.created_at desc')->select();
        
       if(!empty($team1_commission)){

            foreach ($team1_commission as $key => $value) {
                $team1_commission[$key]['time'] = $value['created_at'];
                if($value['type']==1){
                    $team1_commission[$key]['commission_type'] = '二级任务佣金';
                }elseif ($value['type']==2) {
                    $team1_commission[$key]['commission_type'] = '二级会员升级佣金';
                }
            }

        }

        $my_commission = db('income')->alias('t')->field('t.*,u.head_url,u.nick_name,u.real_name,u.phone,u.province,u.city,u.county,u.level,u.account')->join('user_account u','u.id=t.uid')->where($where)->where('t.status=2')->order('t.created_at desc')->select();
        if(!empty($my_commission)){

            foreach ($my_commission as $key => $value) {
                $my_commission[$key]['time'] = $value['update_at'];
                $my_commission[$key]['commission'] =$value['money'];
                $my_commission[$key]['commission_type'] = '个人任务佣金';
              
            }

        }


        $team2_commission = db('team2_commission')->alias('t')->field('t.*,u.head_url,u.nick_name,u.real_name,u.phone,u.province,u.city,u.county,u.level,u.account')->join('user_account u','u.id=t.uid')->where($where)->order('t.created_at desc')->select();
        if(!empty($team2_commission)){

            foreach ($team2_commission as $key => $value) {
                $team2_commission[$key]['time'] = $value['created_at'];
                if($value['type']==1){
                    $team2_commission[$key]['commission_type'] = '一级任务佣金';
                }elseif ($value['type']==2) {
                    $team2_commission[$key]['commission_type'] = '一级会员升级佣金';
                }
            }

        }


        $region_commission = db('region_commission')->alias('t')->field('t.*,u.head_url,u.nick_name,u.real_name,u.phone,u.province,u.city,u.county,u.level,u.account')->join('user_account u','u.id=t.uid')->where($where)->order('t.created_at desc')->select();
        if(!empty($region_commission)){
            foreach ($region_commission as $key => $value) {
                $region_commission[$key]['time'] = $value['created_at'];
                if($value['type']==2){
                    $region_commission[$key]['commission_type'] = '区域代理佣金';
                }
            }
        }

        $data = array_merge($team1_commission,$team2_commission,$region_commission,$my_commission);
        $created_at = array_column($data,'time');
        array_multisort($created_at,SORT_DESC,$data);

        foreach ($data as $key => $value) {

            switch ($value['level']) {
                case '0':
                    $data[$key]['new_level'] = '实习成员';
                    break;
                case '1':
                    $data[$key]['new_level'] = '普通会员';
                break;
                case '2':
                    $data[$key]['new_level'] = '高级会员';
                break;
 
                default:
                    $data[$key]['new_level'] ='';
                    break;
            }

            $data2[$key]['account'] = $data[$key]['account'];

            $data2[$key]['new_level'] = $data[$key]['new_level'];

            $data2[$key]['commission'] = $data[$key]['commission'];

            $data2[$key]['commission_type'] = $data[$key]['commission_type'];

            $data2[$key]['created_at'] = date("Y-m-d H:i",$data[$key]['created_at']);

        }

        $headArr = array("会员账号","会员等级",'收益金额','收益类型',"审核时间");

        if(empty($data2)){
            $this->error('数据为空');
        }
        
        $data3 =array_filter($data2);
        
         $filename='收益记录-'.date("Y-m-d H:i:s");
        (new Export())->ImportExcel($filename,$headArr,$data3);
    } 


}
